package com.irdstudio.efp.console.api.rest;

import java.util.List;
import java.util.Map;

import com.irdstudio.basic.framework.core.constant.PubConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.efp.console.service.facade.SDicTreeService;
import com.irdstudio.efp.console.service.vo.BizParamValueVO;
import com.irdstudio.efp.console.service.vo.SDicTreeVO;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class SDicTreeController extends AbstractController {

	@Autowired
	@Qualifier("sDicTreeService")
	private SDicTreeService sDicTreeService;
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	/**
	 * 分页查询列表
	 * 
	 * @param sDicTreeVo
	 * @return
	 */
	@RequestMapping(value = "/s/dic/page/trees", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<SDicTreeVO>> querySDicTreeAll(@RequestBody SDicTreeVO sDicTreeVo) {
		// 根据用户角色获取用户权限方案
		List<SDicTreeVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		sDicTreeVo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(sDicTreeVo);

		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = sDicTreeService.queryAllOwner(sDicTreeVo);
			break;
		case CurrOrg:
			outputVo = sDicTreeService.queryAllCurrOrg(sDicTreeVo);
			break;
		case CurrDownOrg:
			outputVo = sDicTreeService.queryAllCurrDownOrg(sDicTreeVo);
			break;
		default:
			outputVo = sDicTreeService.queryAllOwner(sDicTreeVo);
			break;
		}
		return getResponseData(outputVo);

	}

	/**
	 * 列表数据查询
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value = "/s/dic/trees", method = RequestMethod.GET)
	public @ResponseBody ResponseData<List<SDicTreeVO>> querySDicTreeAll() {
		List<SDicTreeVO> outputVo = sDicTreeService.queryAll();
		
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键查询详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/s/dic/tree/enname", method = RequestMethod.POST)
	public @ResponseBody ResponseData<SDicTreeVO> queryByPk(@RequestBody SDicTreeVO inSDicTreeVo) {

		SDicTreeVO outputVo = sDicTreeService.queryByPk(inSDicTreeVo);
		return getResponseData(outputVo);

	}

	/**
	 * 分页查询禁入行业详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/s/dic/biz/bid", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<BizParamValueVO>> queryAllBizValueByPage(
			@RequestBody BizParamValueVO inBizParamValueVO) {

		List<BizParamValueVO> outputVo = sDicTreeService.queryAllBizValueByPage(inBizParamValueVO);
		return getResponseData(outputVo);

	}

	/**
	 * 查询禁入行业详情
	 */
	@RequestMapping(value = "/s/dic/value/bid", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<BizParamValueVO>> queryAllBizValue(
			@RequestBody BizParamValueVO inBizParamValueVO) {
		List<BizParamValueVO> outputVo = null;
		if(!StringUtils.isBlank(inBizParamValueVO.getValueId())){
			outputVo = sDicTreeService.queryAllBizValue(inBizParamValueVO);
		}
		return getResponseData(outputVo);
		
	}

	/**
	 * 根据主键删除信息
	 * 
	 * @param sDicTree
	 * @return
	 */
	@RequestMapping(value = "/s/dic/tree", method = RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody SDicTreeVO inSDicTreeVo) {
		int outputVo = sDicTreeService.deleteByPk(inSDicTreeVo);
		return getResponseData(outputVo);

	}

	@RequestMapping(value = "/s/dic/det/biz", method = RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByValueAndEnname(@RequestBody BizParamValueVO inBizParamValueVO) {
		setUserInfoToVO(inBizParamValueVO);
		int outputVo = sDicTreeService.deleteByValueAndEnname(inBizParamValueVO);
		ResponseData<Integer> responseData = new ResponseData<Integer>();
		responseData.setRows(outputVo);
		responseData.setCode(outputVo + "");
		responseData.setSuccess(false);
		if (outputVo == -2) {
			responseData.setMessage("省级有配置，机构禁入行业配置与省级一致，不能删除！");
			return responseData;
		} else if (outputVo == -3) {
			responseData.setMessage("不是本法人机构的禁入行业配置，不能删除！");
			return responseData;
		}
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 * 
	 * @param inSDicTreeVo
	 * @return
	 */
	@RequestMapping(value = "/s/dic/tree", method = RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody SDicTreeVO inSDicTreeVo) {
		int outputVo = sDicTreeService.updateByPk(inSDicTreeVo);
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 * 
	 * @param inSDicTreeVo
	 * @return
	 */
	@RequestMapping(value = "/s/dic/tree", method = RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSDicTree(@RequestBody SDicTreeVO inSDicTreeVo) {
		int outputVo = sDicTreeService.insertSDicTree(inSDicTreeVo);
		return getResponseData(outputVo);

	}
	/**
	 * 查询区域邮编
	 * @return
	 */
	@RequestMapping(value = "/s/dic/trees/area/code", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<SDicTreeVO>> queryAreaCodeAll() {
		String opttype = "STD_GB_T2260";
		List<SDicTreeVO> outputVo = sDicTreeService.querySDicTreeByOpttype(opttype);
		return getResponseData(outputVo);
	}
}
